package com.boolib.animate.framework.service.advice;

import com.boolib.animate.commons.exception.UploadFileException;
import com.boolib.animate.framework.entity.AnimateChapter;
import com.boolib.animate.framework.entity.AnimateWork;
import com.boolib.animate.framework.entity.FileInfo;
import com.boolib.animate.framework.service.base.impl.BaseAnimateChapterContentService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateChapterService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateWorkService;
import com.boolib.animate.framework.service.base.impl.FileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * @author nier
 * @create 2020-02-20 16:20
 */
@Slf4j
@Service
public class AnimateChapterService {

    @Value("${system.uploadpath}")
    private String uploadPath;


    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private BaseAnimateChapterService baseAnimateChapterService;

    @Autowired
    private BaseAnimateChapterContentService baseAnimateChapterContentService;

    @Autowired
    private BaseAnimateWorkService baseAnimateWorkService;


    @Transactional
    public void save(AnimateChapter animateChapter) {
        final AnimateWork animateWork = baseAnimateWorkService.queryById(animateChapter.getWorkId());


        final AnimateChapter save = baseAnimateChapterService.save(animateChapter);
        animateWork.setChapterCount(animateWork.getChapterCount()+1);
        baseAnimateWorkService.updateById(animateWork);



        if(animateChapter.getContents().isEmpty()){
            throw new UploadFileException("你还未上传作品");
        }
        animateChapter.getContents().forEach(x->{
            x.setWorkId(animateChapter.getWorkId());
            x.setChapterId(animateChapter.getId());
        });
        baseAnimateChapterContentService.saveBatch(animateChapter.getContents());
    }





}
